/**
 * @filename:StkotheroutServiceImpl Feb 5, 2025
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import cn.hutool.core.date.DateTime;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.admin.service.IVtuzxKingdeeService;
import com.wonder.app.domain.*;
import com.wonder.app.mapper.StkOtheroutDetailDao;
import com.wonder.app.mapper.StkotheroutDao;
import com.wonder.app.service.StkOtherInDeatilService;
import com.wonder.app.service.StkOtheroutDetailService;
import com.wonder.app.service.StkotheroutService;
import com.wonder.app.request.StkotheroutRequest;
import com.wonder.app.service.SyncKingdeeOrderService;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;

import java.util.*;

import com.wonder.constant.BillType;
import com.wonder.constant.IXqcConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**   
 * @Description:TODO(其他出库单服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class StkotheroutServiceImpl  extends ServiceImpl<StkotheroutDao, Stkotherout> implements StkotheroutService  {
	/**
	 * Logger
	 */
	private static final Logger _logger = LoggerFactory.getLogger(StkotheroutServiceImpl.class);
	@Autowired
	private StkotheroutService stkotheroutService;
	@Autowired
	private StkOtheroutDetailService stkOtheroutDetailService;
	@Autowired
	private StkotheroutDao stkotheroutDao;
	@Autowired
	private StkOtheroutDetailDao stkOtheroutDetailDao;

	@Autowired
	private SyncKingdeeOrderService syncKingdeeOrderService;


	@Autowired
	private IVtuzxKingdeeService vtuzxKingdeeService;

    /**
     * 验证其他出库单信息
     * 
     * @param stkotherout
     * @param type
     * @return String
     */
    @Override
    public String validate(Stkotherout stkotherout,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:其他出库单分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<Stkotherout>   //分页记录
	 */
	@Override 
	public IPage<Stkotherout> selectList(StkotheroutRequest request, Page<Stkotherout> page){
	    LambdaQueryWrapper<Stkotherout> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getId()) {
	       lambdaQueryWrapper.eq(Stkotherout::getId, request.getId());
	      }

		if (StringUtils.hasText(request.getBillNo())) {
			lambdaQueryWrapper.like(Stkotherout::getBillNo, request.getBillNo());
		}

		if (StringUtils.hasText(request.getWeightNo())) {
			lambdaQueryWrapper.like(Stkotherout::getWeightNo, request.getWeightNo());
		}
		if (StringUtils.hasText(request.getUser())) {
			lambdaQueryWrapper.like(Stkotherout::getUser, request.getUser());
		}
		if (request.getDate() != null) {
			Date[] fCREATEDATE = request.getDate();
			if (fCREATEDATE.length > 0) {
				// 设置开始时间为当天的零点
				Calendar startCalendar = Calendar.getInstance();
				startCalendar.setTime(fCREATEDATE[0]);
				startCalendar.set(Calendar.HOUR_OF_DAY, 0);
				startCalendar.set(Calendar.MINUTE, 0);
				startCalendar.set(Calendar.SECOND, 0);
				startCalendar.set(Calendar.MILLISECOND, 0);
				Date startDate = startCalendar.getTime();
				lambdaQueryWrapper.ge(Stkotherout::getDate, startDate);
			}
			if (fCREATEDATE.length == 2) {
				// 设置结束时间为当天的24点
				Calendar endCalendar = Calendar.getInstance();
				endCalendar.setTime(fCREATEDATE[1]);
				endCalendar.set(Calendar.HOUR_OF_DAY, 23);
				endCalendar.set(Calendar.MINUTE, 59);
				endCalendar.set(Calendar.SECOND, 59);
				endCalendar.set(Calendar.MILLISECOND, 999);
				Date endDate = endCalendar.getTime();
				lambdaQueryWrapper.le(Stkotherout::getDate, endDate);
			}
		}
		lambdaQueryWrapper.orderByDesc(Stkotherout::getId);	
		Page<Stkotherout> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }

	/**
	 * receive详情
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> getStkotheroutDetail(Map<String, Object> map) throws Exception{
		String billNo = map.get("billNo").toString();
		Stkotherout stkotheroutform = stkotheroutDao.selectOne(new LambdaQueryWrapper<Stkotherout>()
				.eq(Stkotherout::getBillNo,billNo));
		if (null == stkotheroutform){
			throw new RuntimeException("单号错误请重试");
		}
		List<StkOtheroutDetail> stkotheroutList = stkOtheroutDetailDao.selectList(new LambdaQueryWrapper<StkOtheroutDetail>()
				.eq(StkOtheroutDetail::getBillNo,billNo));
		Map<String,Object> data = new HashMap<>();
		data.put("data",stkotheroutform);
		data.put("dataList",stkotheroutList);
		return data;
	}

	@Override
	public Map<String, Object> getOtherOutList(Map<String, Object> map) {
		int pageIndex = VtuzxUtil.getInt(map,"pageIndex");
		int pageSize = VtuzxUtil.getInt(map,"pageSize");
		String billNo = VtuzxUtil.getString(map,"billNo");//订单号
		List<Stkotherout> dataList = stkotheroutDao.getOtherOutList(pageSize,pageIndex * pageSize,billNo);
		Map<String,Object> data = new HashMap<>();
		data.put("dataList",dataList);
		return data;
	}

	@Override
	public boolean auditOtherOut(Long user_id, String userName, Map<String, Object> map) {
		String billNo = VtuzxUtil.getString(map,"billNo");
		Stkotherout stkotherout = stkotheroutDao.selectOne(new LambdaQueryWrapper<Stkotherout>()
				.eq(Stkotherout::getBillNo,billNo));
		List<StkOtheroutDetail> detailList = stkOtheroutDetailDao.selectList(new LambdaQueryWrapper<StkOtheroutDetail>()
				.eq(StkOtheroutDetail::getBillNo,billNo));
		stkotherout.setStatus("audit");
		stkotherout.setAuditTime(new DateTime());
		stkotherout.setAuditUserName(userName);
		boolean res = stkotheroutService.saveOrUpdate(stkotherout);
		try {
			syncKingdeeOrderService.syncOtherOUT(user_id,userName,stkotherout,detailList);
		}catch (Exception e){
			_logger.info(e.getMessage());
		}
		return res;
	}

	@Override
	public Map<String, Object> getOtheroutDetail(Map<String, Object> map) throws Exception {
		String billNo = map.get("billNo").toString();
		Stkotherout stkotheroutform = stkotheroutDao.selectOne(new LambdaQueryWrapper<Stkotherout>()
				.eq(Stkotherout::getBillNo,billNo)
				.eq(Stkotherout::getStatus,"save"));
		if (null == stkotheroutform){
			throw new RuntimeException("单号错误请重试");
		}
		List<StkOtheroutDetail> stkotheroutList = stkOtheroutDetailDao.selectList(new LambdaQueryWrapper<StkOtheroutDetail>()
				.eq(StkOtheroutDetail::getBillNo,billNo));
		Map<String,Object> data = new HashMap<>();
		data.put("data",stkotheroutform);
		data.put("dataList",stkotheroutList);
		return data;
	}

	/**
	 * 根据物料编码查询仓库信息
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@Override
	public Map<String, Object> getInventoryByMaterial(Long user_id,Map<String, Object> map) throws Exception {
		String locationNo = VtuzxUtil.getString(map,"locationNo");
		if (VtuzxUtil.isEmpty(locationNo)){
			throw new Exception("仓位不能为空");
		}
		List<VtuzxMap> dataList= vtuzxKingdeeService.getInventoryByLocation(user_id,locationNo);

		Map<String,Object> data = new HashMap<>();
		data.put("dataList",dataList);
		return data;
	}

	/**
	 * 创建其他出库申请单
	 * @param user_id
	 * @param user_name
	 * @param stkApply
	 * @param detail 调拨申请详情
	 * @return
	 * @throws Exception
	 */
	@Override
	public Boolean createOtherOut(Long user_id, String user_name, StkApply stkApply, Map<String,Object> detail) throws Exception {

		String BillNo = IXqcConst.createBillNo(BillType.QTCK);
		Stkotherout stkotherout = new Stkotherout();
		stkotherout.setBillNo(BillNo);
		stkotherout.setDate(new Date());
		stkotherout.setDeptNumber(stkApply.getFDEPARTNO());
		stkotherout.setWeightNo(stkApply.getFBILLNO());
		stkotherout.setStatus("save");
		List<StkOtheroutDetail> otheroutDetailList = new ArrayList<>();//保存到其他出库详情的集合
//		for (StkApplyDetail stkApplyDetail : stkApplyDetailList){
			StkOtheroutDetail stkOtheroutDetail = new StkOtheroutDetail();
			stkOtheroutDetail.setBillNo(BillNo);
			stkOtheroutDetail.setMaterialNo(VtuzxUtil.getString(detail,"fmaterialno"));
			stkOtheroutDetail.setMaterialName(VtuzxUtil.getString(detail,"fmaterialname"));
			stkOtheroutDetail.setSpecification(VtuzxUtil.getString(detail,"fspecification"));
			stkOtheroutDetail.setBasicUnitNo(VtuzxUtil.getString(detail,"funitno"));
			stkOtheroutDetail.setBasicUnitName(VtuzxUtil.getString(detail,"funitname"));
//			stkOtheroutDetail.setLot(stkApplyDetail.getflo);
			stkOtheroutDetail.setQty(VtuzxUtil.getDouble(detail,"fqty"));
			stkOtheroutDetail.setStockNo(VtuzxUtil.getString(detail,"fstockno"));
			stkOtheroutDetail.setStockName(VtuzxUtil.getString(detail,"fstockname"));
			stkOtheroutDetail.setLocationNo(VtuzxUtil.getString(detail,"flocationno"));
			stkOtheroutDetail.setLocationName(VtuzxUtil.getString(detail,"flocationname"));
			stkOtheroutDetail.setStkId(VtuzxUtil.getLong(detail,"fid"));
			otheroutDetailList.add(stkOtheroutDetail);

//		}

		//保存其他出库单
		boolean res = stkotheroutService.saveOrUpdate(stkotherout);
		boolean resdetail = stkOtheroutDetailService.saveOrUpdateBatch(otheroutDetailList);
		//保存后直接审核
		Map<String,Object> auditMap = new HashMap<>();
		auditMap.put("billNo",BillNo);
		auditOtherOut(user_id,user_name,auditMap);

		return res && resdetail;
	}

}